//
//  RegisterStepOneView.m
//  Pregnant
//
//  Created by xiangming on 14-4-21.
//  Copyright (c) 2014年 elitework. All rights reserved.
//

#import "RegisterStepOneView.h"
#import "RegisterHelper.h"
#import "ServiceTermsViewController.h"

@implementation RegisterStepOneView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor clearColor];
    self.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    
    [_getVerifyCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_getVerifyCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getVerifyCode setFont:[UIFont systemFontOfSize:12.0f]];

}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    [self.phoneNumField resignFirstResponder];
    [self.verifyCodeInputField resignFirstResponder];
}

- (IBAction)serviceTermAction:(id)sender {
//    if(self.stepBlock)
//    {
//        self.stepBlock(StepTypeServiceTerm);
//    }
    NSLog(@"test");
    if ([self.delegate respondsToSelector:@selector(pushView)]) {
        [self.delegate pushView];
    }
}


- (IBAction)getCodeAction:(id)sender {
    
    if([self.phoneNumField.text isEqualToString:@""]
       ||self.phoneNumField.text == nil){
        [Utils showAlertViewWithTitle:@"请填写手机号码！"];
        return;
    }
    if(![Utils checkPhone:self.phoneNumField.text]){
        [Utils showAlertViewWithTitle:@"手机号码格式错误,请重新填写手机号码！"];
    }
    else
    {
        
        [RegisterHelper shareHelper].registerPhoneNum = self.phoneNumField.text;
        //GCD实现倒计时
        __block int timeout=60; //倒计时时间
        __block RegisterStepOneView *weak_self = self;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        dispatch_source_set_event_handler(_timer, ^{
            if(timeout<=0){ //倒计时结束，关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    //设置界面的按钮显示 根据自己需求设置
                    weak_self.getVerifyCode.userInteractionEnabled = YES;
                    if(weak_self.stepBlock){
                        [self.phoneNumField resignFirstResponder];
                        [weak_self.getVerifyCode setTitle:@"获取验证码" forState:UIControlStateNormal];
                         [weak_self.getVerifyCode setBackgroundColor:MAIN_BLUE_COLOR ];
                    }
                });
            }else{
                //            int minutes = timeout / 60;
                //            int seconds = timeout % 60;
                NSString *strTime = [NSString stringWithFormat:@"%d秒后",timeout];
                dispatch_async(dispatch_get_main_queue(), ^{
                    //设置界面的按钮显示 根据自己需求设置
                    weak_self.getVerifyCode.userInteractionEnabled = NO;
                    [weak_self.getVerifyCode setTitle:strTime forState:UIControlStateNormal];
                    [weak_self.getVerifyCode setBackgroundColor:MAIN_GRAY_COLOR ];
                });
                timeout--;
            }
        });
        dispatch_resume(_timer);
        if (self.delegate && [self.delegate respondsToSelector:@selector(getVerificationCode)]) {
            [self.delegate getVerificationCode];
        }
    }
}


- (IBAction)commitCodeAction:(id)sender {
    
    if([self.verifyCodeInputField.text isEqualToString:@""]
       ||self.verifyCodeInputField.text == nil){
        [Utils showAlertViewWithTitle:@"请输入验证码"];
        return;
    }
    else{
        //检测验证码
        [RegisterHelper shareHelper].verifyCode = self.verifyCodeInputField.text;
        [self.verifyCodeInputField resignFirstResponder];
        if(self.stepBlock){
            self.stepBlock(StepTypeTwo);
        }
    }
}


@end
